package com.micheal.pattern.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by micheal on 2018/3/29/0029.
 * <p>
 * 代理人
 */
public class Agent implements InvocationHandler {

    /**
     * 代理人必须持有被代理人的引用
     */
    private Thing thing;

    public Object getInstance(Thing thing){
        this.thing = thing;
        Class<?> clazz = thing.getClass();
        return Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(),this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("我是被代理人，我现在开始帮你找房子...");
        method.invoke(this.thing,args);
        System.out.println("如果房子合适的话，就准备签合同吧！");
        return null;
    }
}
